.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1989 AT&T
.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
.\" Copyright (c) 2009, Sun Microsystems, Inc.  All Rights Reserved.
.\"
.TH SEM_OPEN 3C "Jul 9, 2009"
.SH NAME
sem_open \- initialize/open a named semaphore
.SH SYNOPSIS
.LP
.nf
#include <semaphore.h>

\fBsem_t *\fR\fBsem_open\fR(\fBconst char *\fR\fIname\fR, \fBint\fR \fIoflag\fR,
     \fB/* unsigned long\fR \fImode\fR, \fBunsigned int\fR \fIvalue\fR */ ...);
.fi

.SH DESCRIPTION
.sp
.LP
The \fBsem_open()\fR function establishes a connection between a named
semaphore and a process (or LWP or thread). Following a call to
\fBsem_open()\fR with semaphore name \fIname\fR, the process may reference the
semaphore associated with \fIname\fR using the address returned from the call.
This semaphore may be used in subsequent calls to \fBsem_wait\fR(3C),
\fBsem_trywait\fR(3C), \fBsem_post\fR(3C), and \fBsem_close\fR(3C). The
semaphore remains usable by this process until the semaphore is closed by a
successful call to \fBsem_close\fR(3C), \fB_Exit\fR(2), or one of the
\fBexec\fR functions.
.sp
.LP
The \fIoflag\fR argument controls whether the semaphore is created or merely
accessed by the call to \fBsem_open()\fR. The following flag bits may be set in
\fIoflag\fR:
.sp
.ne 2
.na
\fB\fBO_CREAT\fR\fR
.ad
.RS 11n
This flag is used to create a semaphore if it does not already exist. If
\fBO_CREAT\fR is set and the semaphore already exists, then \fBO_CREAT\fR has
no effect, except as noted under \fB\fR\fBO_EXCL.\fR Otherwise,
\fBsem_open()\fR creates a named semaphore. The \fBO_CREAT\fR flag requires a
third and a fourth argument: \fImode\fR, which is of type \fBmode_t\fR, and
\fIvalue\fR, which is of type \fBunsigned int\fR. The semaphore is created with
an initial value of \fIvalue\fR. Valid initial values for semaphores are less
than or equal to \fBSEM_VALUE_MAX.\fR
.sp
The user \fBID\fR of the semaphore is set to the effective user \fBID\fR of the
process; the group \fBID\fR of the semaphore is set to a system default group
\fBID\fR or to the effective group \fBID\fR of the process. The permission bits
of the semaphore are set to the value of the \fImode\fR argument except those
set in the file mode creation mask of the process (see \fBumask\fR(2)). When
bits in \fImode\fR other than the file permission bits are specified, the
effect is unspecified.
.sp
After the semaphore named \fIname\fR has been created by \fBsem_open()\fR with
the \fBO_CREAT\fR flag, other processes can connect to the semaphore by calling
\fBsem_open()\fR with the same value of \fIname\fR.
.RE

.sp
.ne 2
.na
\fB\fBO_EXCL\fR\fR
.ad
.RS 11n
If \fBO_EXCL\fR and \fBO_CREAT\fR are set, \fBsem_open()\fR fails if the
semaphore \fIname\fR exists. The check for the existence of the semaphore and
the creation of the semaphore if it does not exist are atomic with respect to
other processes executing \fBsem_open()\fR with \fBO_EXCL\fR and \fBO_CREAT\fR
set. If \fBO_EXCL\fR is set and \fBO_CREAT\fR is not set, the effect is
undefined.
.RE

.sp
.LP
If flags other than \fBO_CREAT\fR and \fBO_EXCL\fR are specified in the
\fIoflag\fR parameter, the effect is unspecified.
.sp
.LP
The \fIname\fR argument points to a string naming a semaphore object. It is
unspecified whether the name appears in the file system and is visible to
functions that take pathnames as arguments. The \fIname\fR argument conforms to
the construction rules for a pathname. The first character of  \fIname\fR must
be a slash  (/) character and the remaining characters of  \fIname\fR cannot
include any slash characters.  For maximum portability,  \fIname\fR should
include no more than 14 characters, but this limit is not enforced.
.sp
.LP
If a process makes multiple successful calls to \fBsem_open()\fR with the same
value for \fIname\fR, the same semaphore address is returned for each such
successful call, provided that there have been no calls to \fBsem_unlink\fR(3C)
for this semaphore.
.sp
.LP
References to copies of the semaphore produce undefined results.
.sp
.LP
The \fBsem_init\fR(3C) function is used with unnamed semaphores.
.SH RETURN VALUES
.sp
.LP
Upon successful completion, the function returns the address of the semaphore.
Otherwise, it will return a value of \fBSEM_FAILED\fR and set \fBerrno\fR to
indicate the error. The symbol \fBSEM_FAILED\fR is defined in the header
\fB<semaphore.h>\fR\&. No successful return from \fBsem_open()\fR will return
the value \fB\fR\fBSEM_FAILED.\fR
.SH ERRORS
.sp
.LP
If any of the following conditions occur, the \fBsem_open()\fR function will
return \fBSEM_FAILED\fR and set \fBerrno\fR to the corresponding value:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 16n
The named semaphore exists and the \fBO_RDWR\fR permissions are denied, or the
named semaphore does not exist and permission to create the named semaphore is
denied.
.RE

.sp
.ne 2
.na
\fB\fBEEXIST\fR\fR
.ad
.RS 16n
\fBO_CREAT\fR and  \fBO_EXCL\fR are set and the named semaphore already exists.
.RE

.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 16n
The \fBsem_open()\fR function was interrupted by a signal.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 16n
The \fBsem_open()\fR operation is not supported for the given name, or
\fBO_CREAT\fR was set in \fIoflag\fR and \fIvalue\fR is greater than
\fBSEM_VALUE_MAX\fR.
.RE

.sp
.ne 2
.na
\fB\fBEMFILE\fR\fR
.ad
.RS 16n
The number of open semaphore descriptors in this process exceeds
\fBSEM_NSEMS_MAX\fR, or the number of open file descriptors in this process
exceeds  \fBOPEN_MAX\fR.
.RE

.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
The length of \fIname\fR string exceeds \fIPATH_MAX\fR, or a pathname component
is longer than \fINAME_MAX\fR while \fB_POSIX_NO_TRUNC\fR is in effect.
.RE

.sp
.ne 2
.na
\fB\fBENFILE\fR\fR
.ad
.RS 16n
Too many semaphores are currently open in the system.
.RE

.sp
.ne 2
.na
\fB\fBENOENT\fR\fR
.ad
.RS 16n
\fBO_CREAT\fR is not set and the named semaphore does not exist.
.RE

.sp
.ne 2
.na
\fB\fBENOSPC\fR\fR
.ad
.RS 16n
There is insufficient space for the creation of the new named semaphore.
.RE

.sp
.ne 2
.na
\fB\fBENOSYS\fR\fR
.ad
.RS 16n
The \fBsem_open()\fR function is not supported by the system.
.RE

.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
MT-Level	MT-Safe
_
Standard	See \fBstandards\fR(7).
.TE

.SH SEE ALSO
.sp
.LP
.BR exec (2),
.BR exit (2),
.BR umask (2),
.BR sem_close (3C),
.BR sem_init (3C),
.BR sem_post (3C),
.BR sem_unlink (3C),
.BR sem_wait (3C),
.BR sysconf (3C),
.BR attributes (7),
.BR standards (7)
